package hz.mobileproject.pda.db;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

import hz.mobileproject.pda.entity.PrisonerPicBean;
import hz.mobileproject.pda.entity.Test;
import hz.mobileproject.pda.entity.request.Request_Edu_inter;
import hz.mobileproject.pda.entity.request.Request_Imp_Call;
import hz.mobileproject.pda.entity.request.Request_Imp_End;
import hz.mobileproject.pda.entity.request.Request_calling;
import hz.mobileproject.pda.entity.request.Request_end;
import hz.mobileproject.pda.entity.response.Response_loadCnf;
import hz.mobileproject.pda.entity.response.Response_loadPoliceInfo;
import hz.mobileproject.pda.entity.response.Response_loadPrisonerList;

/**
 * Created by hz on 2016/8/30.
 */

public class DBManager {

    private final static String dbName = "pda_db";
    private static DBManager mInstance;
    private DaoMaster.DevOpenHelper openHelper;
    private Context context;

    public DBManager(Context context) {
        this.context = context;
        openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
    }

 /*   *
     * 获取单例引用
     *
     * @param context
     * @return*/

    public static DBManager getInstance(Context context) {
        if (mInstance == null) {
            synchronized (DBManager.class) {
                if (mInstance == null) {
                    mInstance = new DBManager(context);
                }
            }
        }
        return mInstance;
    }

/*
    *
     * 获取可读数据库
*/

    private SQLiteDatabase getReadableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getReadableDatabase();
        return db;
    }
/*
    *
     * 获取可写数据库*/

    private SQLiteDatabase getWritableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getWritableDatabase();
        return db;
    }



    //------------------------增加
    /**
     * 用户集合
     *
     * @param black
     */
    public Boolean insertloadCnfList(List<Response_loadCnf> black) {
        if (black == null || black.isEmpty()) {
            return false;
        }
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Response_loadCnfDao userDao = daoSession.getResponse_loadCnfDao();
        userDao.insertInTx(black);
        return true;
    }


    /**
     * 罪犯集合
     *
     */
    public Boolean insertPrisonerList(List<Response_loadPrisonerList> loadPrisonerList) {
        if (loadPrisonerList == null || loadPrisonerList.isEmpty()) {
            return false;
        }
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Response_loadPrisonerListDao userDao = daoSession.getResponse_loadPrisonerListDao();
        userDao.insertInTx(loadPrisonerList);
        return true;
    }

    //用户
    public void insertloadPoliceInfo(Response_loadPoliceInfo loadPoliceInfo) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();

        Response_loadPoliceInfoDao personCleanObjBean = daoSession.getResponse_loadPoliceInfoDao();
        personCleanObjBean.insert(loadPoliceInfo);
    }

    //插入没有发送成功的点名数据
    public void insertcall(Request_calling calling) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();

        Request_callingDao callingDao = daoSession.getRequest_callingDao();
        callingDao.insert(calling);
    }


    //插入没有结束点名
    public void insertrequest_end(Request_end end) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();

        Request_endDao endDao = daoSession.getRequest_endDao();
        endDao.insert(end);
    }


    //插入没有长传成功的录音记录

    public void insertEdu_interDao(Request_Edu_inter end) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();

        Request_Edu_interDao endDao = daoSession.getRequest_Edu_interDao();
        endDao.insert(end);
    }



    //----------------------删除

    /**
     * 删除一条记录
     *
     * @param black
     */
    public void deleteUser(Response_loadCnf black) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Response_loadCnfDao userDao = daoSession.getResponse_loadCnfDao();
        userDao.delete(black);
    }


    /**
     * 根据id,删除数据
     * @param id      用户id
     */
    public void deleteRequest_Edu_inter(long id){
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Request_Edu_interDao userDao = daoSession.getRequest_Edu_interDao();
        userDao.deleteByKey(id);
    }

    //点名
    public void deletePresion() {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Response_loadPrisonerListDao callingDao = daoSession.getResponse_loadPrisonerListDao();
        callingDao.deleteAll();
    }



    //点名
    public void deletecalling() {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Request_callingDao callingDao = daoSession.getRequest_callingDao();
        callingDao.deleteAll();
    }

    //删除本次点名数据
    public void deletecallingRecordId(List<Request_calling> list) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Request_callingDao callingDao = daoSession.getRequest_callingDao();
        callingDao.deleteInTx(list);
    }

    //删除本次指定犯人未点条目
    public void deletecallingByPrisonerId(String prisonerId, String recordId) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Request_callingDao callingDao = daoSession.getRequest_callingDao();
        List<Request_calling> list = callingDao.queryBuilder()
                .where(Request_callingDao.Properties.PrisonerId.eq(prisonerId))
                .where(Request_callingDao.Properties.RecordId.eq(recordId))
                .build().list();
        if(list.size()>0){
            for (Request_calling calling : list){
                callingDao.delete(calling);
            }
        }
    }


    //----------------------修改


    public void updateEduInterUpDown(Request_Edu_inter inter, String value) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Request_Edu_interDao interDao = daoSession.getRequest_Edu_interDao();
        inter.setUp_down(value);
        interDao.update(inter);
    }

    //----------------------查询



    //查询本地是否有点名数据

    /**
     * 查询用户列表
     */
    public List<Request_calling> queryRequest_callingList() {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Request_callingDao callingDao = daoSession.getRequest_callingDao();
        QueryBuilder<Request_calling> qb = callingDao.queryBuilder();
        List<Request_calling> list = qb.list();
        return list;
    }

    /**
     * 1
     * @param record_Id
     */
    public List<Request_calling> queryRequest_callingListRecordId(String record_Id) {
        List<Request_calling> list = new ArrayList<>();
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Request_callingDao callingDao = daoSession.getRequest_callingDao();
        try {
            list = callingDao.queryBuilder().where(Request_callingDao.Properties.RecordId.eq(record_Id)).build().list();
        }catch (Exception e) {

        }
        return list;


    }



    public void deletedbList(List<Request_calling> mdbList) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Request_callingDao callingDao = daoSession.getRequest_callingDao();
        for(int i =0; i < mdbList.size(); i++) {
            Request_calling request_calling = mdbList.get(i);
            callingDao.delete(request_calling);
        }
    }


    /**
     * 查询罪犯列表
     */
    public List<Response_loadPrisonerList> queryPrisonerList() {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Response_loadPrisonerListDao prisonerList = daoSession.getResponse_loadPrisonerListDao();
        QueryBuilder<Response_loadPrisonerList> qb = prisonerList.queryBuilder();
        List<Response_loadPrisonerList> list = qb.list();
        return list;
    }


    //查询教育谈话记录
    public List<Request_Edu_inter> queryRequest_Edu_inter() {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Request_Edu_interDao callingDao = daoSession.getRequest_Edu_interDao();
        QueryBuilder<Request_Edu_inter> qb = callingDao.queryBuilder();
        List<Request_Edu_inter> list = qb.list();
        return list;
    }


    /**
     * 查询用户列表
     */
    public List<Test> queryUserList() {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        TestDao userDao = daoSession.getTestDao();
        QueryBuilder<Test> qb = userDao.queryBuilder();
        List<Test> list = qb.list();
        return list;
    }

    /**
     * 查询用户列表
     */
    public int queryRequest_calling() {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Request_callingDao userDao = daoSession.getRequest_callingDao();
        QueryBuilder<Request_calling> qb = userDao.queryBuilder();
        List<Request_calling> list = qb.list();
        return list.size();
    }


    /**
     * 根据条件查询没有点名结束没上次的数据
     */
    public List<Request_end> queryRequest_end(String black) {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Request_endDao userDao = daoSession.getRequest_endDao();
        QueryBuilder<Request_end> qb = userDao.queryBuilder();
        qb.where(Request_endDao.Properties.Id.gt(black)).orderAsc(Request_endDao.Properties.Id);
        List<Request_end> list = qb.list();
        return list;
    }

    /**
     * 根据条件查询没有点名结束没上次的数据
     */
    public List<Request_end> queryRequest_ends() {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Request_endDao userDao = daoSession.getRequest_endDao();
        QueryBuilder<Request_end> qb = userDao.queryBuilder();
        List<Request_end> list = qb.list();
        return list;
    }

    /**
     * 重点部位
     * 插入重点部位 清点没有上传成功的数据
     * @param request_imp_call
     */
    public void insertImpBuWeiCalling(Request_Imp_Call request_imp_call) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Request_Imp_CallDao request_imp_callDao = daoSession.getRequest_Imp_CallDao();
        request_imp_callDao.insert(request_imp_call);
    }

    /**
     * 重点部位
     * 查询清点没有上传成功的集合数据
     */
    public List<Request_Imp_Call> queryRequest_Imp_CallingList() {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Request_Imp_CallDao request_imp_callDao = daoSession.getRequest_Imp_CallDao();
        QueryBuilder<Request_Imp_Call> request_imp_callQueryBuilder = request_imp_callDao.queryBuilder();
        List<Request_Imp_Call> list = request_imp_callQueryBuilder.list();
        return list;

    }

    /**
     *重点部位
     * 删除重点部位, 没有上传成功的表
     */
    public void deleteErrorCalling() {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Request_Imp_CallDao request_imp_callDao = daoSession.getRequest_Imp_CallDao();
        request_imp_callDao.deleteAll();
    }

    /**
     * 重点部位
     * 插入重点部位 结束没上传的数据
     * @param request_imp_end_error
     */
    public void insertImpEnd(Request_Imp_End request_imp_end_error) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Request_Imp_EndDao request_imp_endDao = daoSession.getRequest_Imp_EndDao();
        request_imp_endDao.insert(request_imp_end_error);
    }
    /**
     * 重点部位
     * 查询所有没结束的 数据
     */
    public List<Request_Imp_End> queryImpEnd() {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        Request_Imp_EndDao request_imp_endDao = daoSession.getRequest_Imp_EndDao();
        QueryBuilder<Request_Imp_End> qb = request_imp_endDao.queryBuilder();
        return qb.list();

    }

    /**--------------↓ 帶照片的数据库表的操作 ↓---------------*/
    public void insertPrisonerPicBeanList(PrisonerPicBean prisonerPicBean) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        daoSession.getPrisonerPicBeanDao().insert(prisonerPicBean);
    }

    public void insertPrisonerPicBean(PrisonerPicBean prisonerPicBean) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        daoSession.getPrisonerPicBeanDao().insert(prisonerPicBean);
    }

    public void deletePrisonerPicBean(PrisonerPicBean prisonerPicBean) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        daoSession.getPrisonerPicBeanDao().delete(prisonerPicBean);
    }

    public void updatePrisonerPicBean(PrisonerPicBean prisonerPicBean,String newLocalPath) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        prisonerPicBean.localPic = newLocalPath;
        daoSession.getPrisonerPicBeanDao().update(prisonerPicBean);
    }

    public List<PrisonerPicBean> queryPrisonerPicList(){
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        List<PrisonerPicBean> list = daoSession.getPrisonerPicBeanDao().queryBuilder().build().list();
        return list;
    }

//    public PrisonerPicBean queryPrisonerPicBean(String prisoner_id) {
//        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
//        DaoSession daoSession = daoMaster.newSession();
//        PrisonerPicBean unique = daoSession.getPrisonerPicBeanDao().queryBuilder().where(PrisonerPicBeanDao.Properties.Prisoner_id.eq(prisoner_id)).build().unique();
//        return unique;
//    }

    public PrisonerPicBean queryPrisonerPicBean(String prisoner_id) {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        List<PrisonerPicBean> list = daoSession.getPrisonerPicBeanDao().queryBuilder().where(PrisonerPicBeanDao.Properties.Prisoner_id.eq(prisoner_id)).build().list();
        PrisonerPicBean unique = new PrisonerPicBean();
        if (list.size() > 0 && list.get(0) != null){
            unique = list.get(0);
        }
        return unique;
    }


}
